<?php
/**
 * 获取
 *
 * 
 */
 
use Workerman\Worker;
use Workerman\Mysql;

class set_poster_contents{
	
	public function entry($param){
		//var_dump($param);
		return $this->do_set_poster_contents($param);
	}

	
	/** 
	* do_set_poster_contents  
	* 
	* 
	* @access private 
	* @param $param 
	* @since 1.0 
	* @return array(errcode,errmsg,rtv) 
	*/
	private function do_set_poster_contents($param){

		$submission = @$param["submission"];

        if(!empty($param["user_id"])){
            $user_id = $param["user_id"];
        }else{
            $userObj= Token::getToken($_SERVER['HTTP_ACCESS_TOKEN']);
            $user_id = $userObj[0]['id'];
        }

        $content = $param["content"];


		$sql = "SELECT c.id as department_id,c.department_headquarters_id,b.class FROM hf_mes_user a 
				INNER JOIN hf_mes_groups b ON a.groups=b.id 
				INNER JOIN hf_mes_department c ON a.department=c.id
				WHERE a.id='$user_id'";
		$dp = sql_query($sql);
		$department_id = $dp[0]["department_id"];
		$department_headquarters_id = $dp[0]["department_headquarters_id"];
		$group = $dp[0]["class"];
		// group为1、2、3,分别代表采购员、副总监、总监
		if($submission == 1){
			// 部门点击提交稿件按钮
			$poster_contents_id = $param["poster_contents_id"];
		/*	echo '*******************';
			var_dump($group);
			var_dump($department_headquarters_id);
			var_dump($department_id);
			echo '*******************';*/

			$sql_str = " AND c.id='$user_id'";
			if($group == "副总监"){
				$sql_str = " AND c.id='$user_id'";
			}elseif($group == "总监"){
				$sql_str = " AND b.department_id IN (SELECT id FROM hf_mes_department WHERE department_headquarters_id='$department_headquarters_id')";
			}
			
			// 查看该部门下所有稿件是否校稿  原来上一位程序员就这样写 看不懂重构吧
			$sql = "SELECT b.id,b.contents,b.finish,b.user_contents,b.deputy_director,d.name,e.name as department_name,d.id as hf_mes_poster_management_id FROM hf_mes_poster_picture a 
					INNER JOIN hf_mes_poster_picture_department b ON a.id = b.picture_id 
					INNER JOIN hf_mes_poster_contents f ON a.poster_contents_id = f.id 
					INNER JOIN hf_mes_user c ON b.department_id=c.department
					INNER JOIN hf_mes_poster_management d ON f.poster_id=d.id
					INNER JOIN hf_mes_department e ON b.department_id=e.id
					WHERE a.poster_contents_id= $poster_contents_id $sql_str";
		 	$ret = sql_query($sql);
		 	//var_dump($ret);die;
			if(count($ret) <= 0){
				return array("errcode"=>4002, "errmsg"=>"获取部门稿件失败", "rtval"=>"");
			}
			$finish = 1;
			$sql_str =  "";
            $sql_str_poster_ids =  "";
			foreach($ret as $values){

			    if($group == "采购员"){

                    if($values["contents"] == Null || $values["contents"] == "" || count(json_decode($values["contents"])) == 0){
                        $errmsg = '';
                        $finish = 0;
                        $errmsg .= $values['department_name'].'-'.$values['name'].';';
                    }

                }elseif ($group == "副总监"){

                    if($values["finish"] == 0 || $values["user_contents"] == "" || count(json_decode($values["user_contents"])) == 0){
                        $errmsg = '';
                        $finish = 0;
                        $errmsg .= $values['department_name'].'-'.$values['name'].';';
                    }

                }elseif ($group == "总监"){

                    if($values["finish"] == 0 || $values["user_contents"] == "" || count(json_decode($values["user_contents"])) == 0 || $values["deputy_director"]=='-1'){
                        $errmsg = '';
                        $finish = 0;
                        $errmsg .= $values['department_name'].'-'.$values['name'].';';
                    }
                }

				$sql_str .= $values["id"].",";

                $sql_str_poster_ids .= $values["hf_mes_poster_management_id"].",";
			}

			$sql_str = rtrim($sql_str,",");
			//var_dump($finish);die;
			if($finish == 0){
				return array("errcode"=>4002, "errmsg"=>'还有部门没完成校稿: '.$errmsg, "rtval"=>"");
			}

			$sql = "update hf_mes_poster_picture_department set finish=1,finish_date='".date("Y-m-d H:i:s")."',user_id='$user_id' WHERE id in ($sql_str)  AND finish=0";

			if($group == "副总监"){
				$sql = "update hf_mes_poster_picture_department set deputy_director='$user_id' WHERE id in ($sql_str)";
			}elseif($group == "总监"){
			    //总监提交 修改为已完成  hf_mes_poster_management

                $sql_str_poster_ids = rtrim($sql_str_poster_ids,",");
                $sql1 = "update hf_mes_poster_management set status='finish' WHERE id in ($sql_str_poster_ids)";

                sql_query($sql1);

				$sql = "update hf_mes_poster_picture_department set chief_inspector='$user_id' WHERE id in ($sql_str)";
			}
			$rt = sql_query($sql);
			$this->update_history_contents($group,$sql_str);
			$this->send_email($group,$poster_contents_id,$department_id,$department_headquarters_id);
		}elseif ($submission == 3){

  	        // 单张海报点击保存按钮
			$picture_department_id = $param["picture_department_id"];
			$contents = $param["contents"];

			$sql_str  ="contents='$contents'";

			$sql = "update hf_mes_poster_picture_department set $sql_str  WHERE id='$picture_department_id'";
			$rt = sql_query($sql);

        }else{

			// 单张海报点击保存按钮
			$picture_department_id = $param["picture_department_id"];
            $picture_id = $param["picture_id"];
			$contents = json_encode($param["contents"],JSON_UNESCAPED_UNICODE);

            //$contents_confirm = json_encode($param["contents_confirm"],JSON_UNESCAPED_UNICODE);
			// 如果用户角色是采购员和副总监，则保存历史数据
			//$sql_str  ="user_id='$user_id',contents='$contents',contents_confirm='$contents_confirm'";
			$sql_str  ="user_id='$user_id',contents='$contents'";
			if($group != "总监"){
				
				if($group == "副总监"){
					$sql_str.= ",deputy_director_contents='$contents'";
				}else{
					$sql_str.= ",user_contents='$contents'";
				}
			}
			$sql = "update hf_mes_poster_picture_department set $sql_str  WHERE id='$picture_department_id'";
			$rt = sql_query($sql);

		}

        return array("errcode"=>0, "errmsg"=>"", "rtval"=>"");
		
	}

	/** 
	* check_poster_finish  
	* 判断该部门下稿件都是否校稿完成
	* 
	* @access private 
	* @param $poster_contents_id,$sql_str
	* @since 1.0 
	* @return Boolean
	*/
	private function check_poster_finish($ret){
		
		$finish = 1;
		foreach($ret as $values){
			if($values["contents"] == Null || $values["contents"] == "" || count(json_decode($values["contents"])) == 0){
				$finish = 0;
			}
		}
		return $finish;
	}

	/** 
	* get_picture_department_id  
	* 获取picture_department_id
	* 
	* @access private 
	* @param $ret
	* @since 1.0 
	* @return string 
	*/
	private function get_picture_department_id($ret){
		
		$sql_str =  "";
		foreach($ret as $values){
			$sql_str .= $values["id"].",";
		}
		$sql_str = rtrim($sql_str,",");
		return $sql_str;
	}

	/** 
	*   更新历史修改内容数据
	* 
	* 1、如果是采购员，就更新内容到user_contents
	* 2、如果是副总监，就更新内容到deputy_director_contents
	*
	* @access private 
	* @param $ret
	* @since 1.0 
	* @return string 
	*/
	private function update_history_contents($group,$sql_str){

		if($group != "总监"){
			$picture_department_ids = explode(",",$sql_str);
			foreach($picture_department_ids as $picture_department_id){
				$sql = "SELECT contents FROM hf_mes_poster_picture_department WHERE id='$picture_department_id'";
				$ret = sql_query($sql);
				$history_contents = $ret[0]["contents"];
				$sql = "update hf_mes_poster_picture_department set user_contents='$history_contents' WHERE id='$picture_department_id'"; 
				if($group == "副总监"){
					$sql = "update hf_mes_poster_picture_department set deputy_director_contents='$history_contents' WHERE id='$picture_department_id'";
				}
				sql_query($sql);
				
			}
			

		}
		
		
		
		
	}

	/** 
	* send_email  
	* 发送邮件
	* 1、如果是采购员，就发邮件给副总监;
	* 2、如果是副总监，判断该总部门下所有分部门是否都校稿完成，如果都校稿完成，则发邮件给总监;
	* 3、如果是总监，判断当次校稿所有稿件都校稿完成，如果都校稿完成,则发邮件给市场部
	*
	* @access private 
	* @param $ret
	* @since 1.0 
	* @return string 
	*/
	private function send_email($group,$poster_contents_id,$department_id,$department_headquarters_id){


		if($group == "副总监"){
			$sql = "SELECT b.id FROM hf_mes_poster_picture a 
					INNER JOIN hf_mes_poster_picture_department b ON a.id = b.picture_id 
					WHERE a.poster_contents_id= $poster_contents_id AND b.department_id IN (SELECT id FROM hf_mes_department WHERE department_headquarters_id='$department_headquarters_id') AND b.deputy_director='-1'";
			$ret = sql_query($sql);
			$sql = "";
			if(count($ret) <= 0){
				$sql = "SELECT a.email FROM hf_mes_user a 
						INNER JOIN hf_mes_groups b ON a.groups=b.id  
						WhERE a.department IN (SELECT id FROM hf_mes_department WHERE department_headquarters_id='$department_headquarters_id') AND b.class='总监'";
			}

		}elseif($group == "总监"){
			$sql = "SELECT b.id FROM hf_mes_poster_picture a 
					INNER JOIN hf_mes_poster_picture_department b ON a.id = b.picture_id 
					WHERE a.poster_contents_id= $poster_contents_id AND  b.chief_inspector='-1'";
			$ret = sql_query($sql);
			$sql = "";
			if(count($ret) <= 0){
				$sql = "SELECT a.email FROM hf_mes_user a 
						INNER JOIN hf_mes_groups b ON a.groups=b.id  
						WhERE  b.class='设计师'";
			}
		}else{
			$sql = "SELECT a.email FROM hf_mes_user a 
					INNER JOIN hf_mes_groups b ON a.groups=b.id  
					WhERE a.department='$department_id' AND b.class='采购员'";
		}
		if($sql != ""){
			$email_arr = [];
			$dp = sql_query($sql);
			foreach($dp as $val){
				array_push($email_arr,$val["email"]);
			}
			$flag = sendMail($email_arr, '有稿件待校', '请用手机登录校稿系统，进行校稿,网址:http://www.sctmes.com:6060/mobile_web/login.php');
			if ($flag) {
				$obj = "done";
			} else {
				$obj = "邮件发送失败！";
			}
		}
		
	}

	 
	 
	/** 
	* create  
	* 通过 call_user_func 获得本class的实例
	* 
	* @access public 
	* @since 1.0 
	* @return object 本类的实例
	*/	
    public static function create(){  
        static $instance;
        if (!$instance){  
			$instance = new set_poster_contents();  
        }
		return $instance;  
    }   
 }
 

?>